package com.yxj04;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ListDemo {
    public static void main(String[] args) {
        //创建List集合对象
        List<Student> list = new ArrayList<>();
        //创建学生对象
        Student s1 = new Student("yyy",23);
        Student s2 = new Student("xxx",30);
        Student s3 = new Student("jjj",3);
        //把学生添加到集合
        list.add(s1);
        list.add(s2);
        list.add(s3);
        //3种方式遍历集合
        //1.迭代器，集合持有的遍历方式
        Iterator<Student> it = list.iterator();
        while(it.hasNext()) {
            Student s = it.next();
            System.out.println(s.getName()+","+s.getAge());
        }
        System.out.println("-----------");
        //2.普通for循环遍历
        for(int i = 0; i < list.size(); i++) {
            Student s = list.get(i);
            System.out.println(s.getName()+","+ s.getAge());
        }
        System.out.println("-----------");

        //3.增强for循环方式，最方便的循环方式
        for (Student s : list) {
            System.out.println(s.getName()+","+s.getAge());
        }
    }
}
